package com.dt76.smallloanbackendservice.service.afterloan.impl;


import com.dt76.smallloanbackendservice.mapper.afterloan.RepaymentMapper;
import com.dt76.smallloanbackendservice.pojo.afterloan.*;
import com.dt76.smallloanbackendservice.service.afterloan.RepaymentService;
import com.dt76.smallloanbackendservice.utils.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("RepaymentService")
public class RepaymentServiceImpl implements RepaymentService {

    private final RepaymentMapper repaymentMapper;

    public RepaymentServiceImpl(RepaymentMapper repaymentMapper) {
        this.repaymentMapper = repaymentMapper;
    }

    @Override
    public PageResult qryAll(Integer cuPage, Integer size, RepaymentQualification q) {
        Page page = PageHelper.startPage(cuPage, size);
        List<CommonLoanInfo> list = repaymentMapper.getPageList(q);
        PageInfo info = new PageInfo<>(page.getResult());
        return new PageResult(info.getTotal(), list);
    }

    @Override
    public List<RepaymentInfo> getById(Integer id) {
        List<RepaymentInfo> infos = repaymentMapper.qryById(id);
        for (RepaymentInfo info : infos) {
            info.setContractAmount(info.getInterest() + info.getPrincipal() + info.getManagementFee() - info.getReductionAmount());
        }
        return infos;
    }

    @Override
    public Integer modifyCommitInfo(Integer repaymentId, RepaymentCommitInfo info) {
        info.setRepaymentId(repaymentId);
        return repaymentMapper.modifyCommitInfo(info);
    }

    @Override
    public Integer modifyPrepaymentInfo(PrepaymentCommitInfo q, Integer loanId) {
        q.setLoanId(loanId);
        return repaymentMapper.modifyPrepaymentInfo(q);
    }
}
